package com.credit.jumu.calculator.fragment;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

import com.credit.jumu.calculator.R;
import com.credit.jumu.calculator.base.BaseFragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * Created by Administrator on 2017/11/22/022.
 */
@EFragment(R.layout.fragment_home)
public class HomeFragment extends BaseFragment {
	FragmentManager fm;
	public CarFragment carFragment;
	public HouseFragment houseFragment;
	public OtherFragment otherFragment;

	@AfterViews
	public void initView() {

		fm = getFragmentManager();
		showFragment(1);
	}

	@ViewById
	TextView mycartext, myhousetext, myothertext, homeheadtext;

	@Click(R.id.myhousetext)
	public void myhousetext() {
		hideKeyboard(getActivity());
		chooseType(1);
	}

	@Click(R.id.mycartext)
	public void carClick() {
		hideKeyboard(getActivity());
		chooseType(2);
	}

	@Click(R.id.myothertext)
	public void myothertext() {
		hideKeyboard(getActivity());
		chooseType(3);
	}

	public void chooseType(int index) {
		switch (index) {
			case 1:
				myhousetext.setTextColor(ContextCompat.getColor(getContext(), R.color.tabtextbg));
				myhousetext.setBackgroundResource(R.drawable.house_choosebg);

				mycartext.setTextColor(ContextCompat.getColor(getContext(), R.color.mywhlie));
				mycartext.setBackgroundResource(R.drawable.car_unchoosebg);

				myothertext.setTextColor(ContextCompat.getColor(getContext(), R.color.mywhlie));
				myothertext.setBackgroundResource(R.drawable.other_unchoosebg);
				homeheadtext.setText("房贷计算器");
				showFragment(index);
				break;
			case 2:
				myhousetext.setTextColor(ContextCompat.getColor(getContext(), R.color.mywhlie));
				myhousetext.setBackgroundResource(R.drawable.house_unchoosebg);

				mycartext.setTextColor(ContextCompat.getColor(getContext(), R.color.tabtextbg));
				mycartext.setBackgroundResource(R.drawable.car_choosebg);

				myothertext.setTextColor(ContextCompat.getColor(getContext(), R.color.mywhlie));
				myothertext.setBackgroundResource(R.drawable.other_unchoosebg);
				homeheadtext.setText("车贷计算器");
				showFragment(index);
				break;
			case 3:
				myhousetext.setTextColor(ContextCompat.getColor(getContext(), R.color.mywhlie));
				myhousetext.setBackgroundResource(R.drawable.house_unchoosebg);

				mycartext.setTextColor(ContextCompat.getColor(getContext(), R.color.mywhlie));
				mycartext.setBackgroundResource(R.drawable.car_unchoosebg);

				myothertext.setTextColor(ContextCompat.getColor(getContext(), R.color.tabtextbg));
				myothertext.setBackgroundResource(R.drawable.other_choosebg);
				homeheadtext.setText("其他贷款计算器");
				showFragment(index);
				break;
		}
	}

	public void showFragment(int index) {
		FragmentTransaction ft = fm.beginTransaction();
		hideFragment(ft);
		switch (index) {
			case 1:
				if (houseFragment == null) {
					houseFragment = new HouseFragment_();
					ft.add(R.id.homefragments, houseFragment);
				} else {
					ft.show(houseFragment);
				}
				break;
			case 2:
				if (carFragment == null) {
					carFragment = new CarFragment_();
					ft.add(R.id.homefragments, carFragment);
				} else {
					ft.show(carFragment);
				}
				break;
			case 3:
				if (otherFragment == null) {
					otherFragment = new OtherFragment_();
					ft.add(R.id.homefragments, otherFragment);
				} else {
					ft.show(otherFragment);
				}
				break;
		}
		ft.commit();
	}

	public void hideFragment(FragmentTransaction fragmentTransaction) {
		if (null != houseFragment) {
			fragmentTransaction.hide(houseFragment);
		}
		if (null != carFragment) {
			fragmentTransaction.hide(carFragment);
		}
		if (null != otherFragment) {
			fragmentTransaction.hide(otherFragment);
		}
	}
}
